package schoolmath.web.components.content;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import schoolmath.web.entities.User;
import schoolmath.web.entities.UserGroup;
import schoolmath.web.services.SelectService;

import java.util.List;

@RequiresRoles(value = {"Администратор", "Преподаватель"}, logical = Logical.OR)
public class ManageUserGroupUnitsContent {

	private static Logger logger = LoggerFactory.getLogger(ManageUsersContent.class);
	@Persist(PersistenceConstants.FLASH)
	private UserGroup userGroup;
	private List<User> users;
    @Property
    private User user;
	private SelectModel usersSelectModel;
	@Inject
	private Session session;
	@Inject
	private SelectService selectService;

	public UserGroup getUserGroup() {
		return this.userGroup;
	}

	public void setUserGroup(UserGroup userGroup) {
		this.userGroup = userGroup;
	}

	@SuppressWarnings("unchecked")
	public List<User> getUsers() {
		if (users == null) {
			users = session.createCriteria(User.class).list();
		}
		return users;
	}

	public SelectModel getUsersSelectModel() {
		if (usersSelectModel == null) {
			usersSelectModel = selectService.createSelectModel(selectService.createOptionModelList(getUsers()));
		}
		return usersSelectModel;
	}

	public Object onRefreshPage() {
		setUserGroup(userGroup);
		return this;
	}
    public String getName(){
        return user.getName();
    }
    public int getId(){
        return user.getId();
    }

}
